package com.delio.client.pagos.pagoadelantos;

import java.util.Date;

import com.delio.client.idioma.DelioConstants;

import com.delio.client.pagos.pagosueldos.PanelGridPagoSueldo;
import com.delio.client.util.ColumnField;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;

public class PanelNuevoPagoAdelanto extends FormPanel {
	private DelioConstants k = GWT.create(DelioConstants.class);
	private Button btnRegistrar;
	private HiddenField<String> id;
	private DateField fecha;
	private NumberField txtMonto;
	private TextArea txtDescripcion;

	private FormButtonBinding butonbinding;
	private FormBinding formbinding;
	

	public PanelNuevoPagoAdelanto() {
		super();
		setHeaderVisible(false);
		setBodyBorder(false);
		setLabelAlign(LabelAlign.TOP);
		
		PanelGridPagoAdelanto.panelnuevopagoadelanto=this;
		PanelGridPagoSueldo.panelnuevopagoadelanto=this;
		
		setHabilitar(false);
		
		formbinding=new FormBinding(this);
		
		
		btnRegistrar = new Button(k.registrar());
		btnRegistrar.setSize(80, 30);
		btnRegistrar.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				save();

			}
		});

		
		butonbinding = new FormButtonBinding(this);
		butonbinding.addButton(btnRegistrar);
		
		
		id = new HiddenField<String>();

		fecha = new DateField();
		fecha.setValue(new Date());
		fecha.setAllowBlank(false);
		fecha.setFieldLabel(k.fechapago());

		txtMonto = new NumberField();
		txtMonto.setFieldLabel(k.monto());
		txtMonto.setAllowBlank(false);
		txtMonto.setPropertyEditorType(Double.class);
		txtMonto.setMinLength(1);
		txtMonto.focus();

		txtDescripcion = new TextArea();
		txtDescripcion.setEmptyText(k.escribaobservacion());
		txtDescripcion.setHideLabel(true);

		ColumnField cf = new ColumnField();
		cf.setDerechaTop(fecha, new FormData("90%"));
		cf.setDerechaTop(txtMonto, new FormData("80%"));

		add(id);
		add(cf);
		add(txtDescripcion, new FormData("90%"));

		addButton(btnRegistrar);
		setButtonAlign(HorizontalAlignment.CENTER);

		 setStore(PagoAdelantos.store);
	}

	
	public void setStore(ListStore<BeanModel> store) {

		formbinding.addFieldBinding(new FieldBinding(id, "id"));
		formbinding.addFieldBinding(new FieldBinding(fecha, "fecha"));
		formbinding.addFieldBinding(new FieldBinding(txtMonto, "monto"));
		formbinding.addFieldBinding(new FieldBinding(txtDescripcion, "descripcion"));
		
		formbinding.setStore(store);
		
	}

	@Override
	public void reset() {
		id.clear();
		txtMonto.clear();
		txtDescripcion.clear();
	    fecha.setValue(new Date());
		
		PanelGridPagoAdelanto.grid.getSelectionModel().deselectAll();
		unbind();

	}

	public void bind(BeanModel selectedModel) {
		formbinding.bind(selectedModel);
		setHeading(k.ModificarEmpleado());

	}

	public void unbind() {
		formbinding.unbind();
		setHeading(k.NuevoEmpleado());
	}
	
	
	private void save() {

		PagoAdelanto pa = new PagoAdelanto();
		if(id.getValue()!=null)
		 pa.setId(Integer.valueOf(id.getValue()));
		
		pa.setMonto(txtMonto.getValue().doubleValue());
		pa.setFecha(fecha.getValue());
		pa.setDescripcion(txtDescripcion.getValue());

		PagoAdelantos.save(pa);
		PagoAdelantos.panelnuevopagoadelanto=this;

	}
	
	public void setHabilitar(Boolean valor){
		
		setEnabled(valor);
	}

}
